package heima.com.service;

import heima.com.dao.StudentMapper;
import heima.com.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentService {

    public List<Student> findAll(String name, String password) {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> list = mapper.findAll(name, password);
            System.out.println(list);
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    public Boolean login(String name, String password) {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession session = sqlSessionFactory.openSession();
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.findStudentByNameAndPwd(name, password);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;

    }
}
